#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

void help()
{
	printf("\nThis program demonstrates using features2d detector only\n"
			"Using the user-specified detector:\n"
			"\n"
			"Usage:\n detector_simple <detector> <image>\n");
}


int main(int argc, char** argv)
{
	if(argc != 3)
	{
		help();
		return -1;
	}

	GoodFeaturesToTrackDetector::Params paramsGTFF;
	//SIFT::DetectorParams paramsSIFT(SIFT::DetectorParams::GET_DEFAULT_THRESHOLD(), SIFT::DetectorParams::GET_DEFAULT_EDGE_THRESHOLD());
	//double paramSURF = 200;
	//int paramFAST = 20;
	//struct CvMSERParams paramMSER = cvMSERParams();
	// color parameters
	//paramMSER.maxEvolution *= 5;
	//paramMSER.minDiversity *= 10.0;
#if 0
	paramMSER.edgeBlurSize *= 2;
	paramMSER.edgeBlurSize |= 1; // must be odd
//#else
	paramMSER.edgeBlurSize = 0; // no blurring
#endif
	//paramMSER.minMargin *= 100.0;
	//paramMSER.maxArea = INT_MAX;
	//paramMSER.minArea = 0;
	//paramMSER.areaThreshold += 0.5;
	struct CvStarDetectorParams paramStar = cvStarDetectorParams();
	//paramStar.responseThreshold *= 2.4;
	//paramStar.suppressNonmaxSize = 15;
	//paramStar.lineThresholdProjected *=5;
	//paramStar.lineThresholdBinarized *=5;

	Ptr<FeatureDetector> detector = FeatureDetector::create(string(argv[1]), (void*)&paramStar);
	if (detector == 0)
	{
		printf ("Unrecognized detector name, try GFTT, HARRIS, FAST\n");
		return -1;
	}

	Mat img = imread(argv[2], /* CV_LOAD_IMAGE_COLOR */ CV_LOAD_IMAGE_GRAYSCALE );
	if(img.empty())
	{
		printf("Can't read one of the images\n");
		return -1;
	}

	vector<KeyPoint> kpv;
	int64 t = getTickCount();
	detector->detect(img, kpv);

	printf ("Num. of Key Points detected: %d;\t%ld secs", kpv.size(), (int64)(getTickCount()  - t) / static_cast<int64>(getTickFrequency()));
	Mat res =img.clone();
	drawKeypoints(img, kpv, res, Scalar(255, 0, 0), DrawMatchesFlags::DRAW_RICH_KEYPOINTS | DrawMatchesFlags::NOT_DRAW_KEYPOINT_SIZE);

	// drawing the results
	namedWindow("source", 1);
	namedWindow("result", 1);
	imshow("source", img);
	imshow("result", res);

	waitKey(0);

	return 0;
}
